﻿using System;
using System.Collections.Generic;
using System.Text;

namespace adb
{
    /// <summary>
    /// API请求回调
    /// </summary>
    public class ResultApi<T>
    {
        /// <summary>
        /// 处理状态
        /// </summary>
        public bool success { get; set; }
        /// <summary>
        /// 状态描述
        /// </summary>
        public string msg { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        public T data { get; set; }
        /// <summary>
        /// 查询总数，默认为０
        /// </summary>
        public int dataCount { set; get; } = 0;

    }
    /// <summary>
    /// 
    /// </summary>
    public class ResultApi {
        /// <summary>
        /// 出错
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ResultApi<T> error<T>(T model,string msg = "操作失败")
        {
            return new ResultApi<T> { success = false, msg = msg,data = model };
        }
        /// <summary>
        /// 成功回调
        /// </summary>
        /// <param name="model">传值</param>
        /// <returns></returns>
        public static ResultApi<T> ok<T>(T model, string msg = "操作成功")
        {
            return new ResultApi<T> { data = model, success = true, msg = msg,dataCount = 0 };
        }
        /// <summary>
        /// 成功回调,带返回行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static ResultApi<T> ok<T>(T model, int count)
        {
            return new ResultApi<T> { data = model, success = true, msg = "操作成功",dataCount = count };
        }
        /// <summary>
        /// 自行判断返回相应的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ok"></param>
        /// <param name="model"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ResultApi<T> okOrError<T>(bool ok, T model = default(T), string msg = null) { 
            if(ok)return ResultApi.ok(model,msg??"操作成功");
            return ResultApi.error(model,msg??"操作失败");
        }

        
    }

}
